package com.wangyg.juc.ProducerConsumer.version2;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by wangyg6 on 2018/7/11.
 */
public class Producer implements Runnable {
	private List<String> list;
	private static AtomicInteger integer = new AtomicInteger();

	public Producer setList(List<String> list) {
		this.list = list;
		return this;
	}

	@Override
	public void run() {
		while (true) {
			synchronized (list) {
				if (list.size() >= 10) {//满了
					try {
						list.notifyAll();
						list.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				if (list.size() <= 10) {//唤醒时会出现多于设置容量
					int i = integer.incrementAndGet();
					list.add(String.valueOf(i));
					System.out.println(Thread.currentThread().getName() + "生产了" + i + "____" + list.size());
				}
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

